<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
var beautySum = function(s) {
    const len = s.length;
    let result = 0;
    // 第一层循环
    for (let i = 0; i < len; i++) {
        // 设置 map 存储每一个字母的出现次数
        const map = {};
        // 第二层循环
        for (let j = i; j < len; j ++) {
            // 记录新增的字符串，加到 map 中
            map[s[j]] = (map[s[j]] || 0) + 1;
            // 获取结果数组
            const values = Object.values(map);
            // 获取最大最小值
            const max = Math.max(...values), min = Math.min(...values);
            // 计算美丽值
            result += max - min;
        }
    }
    return result;
};
console.log(beautySum("aabcbaa"));
  </script>
</body>
</html>